//
//  CompanyResumeManagementViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/30.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyResumeManagementViewController.h"
#import "LYTitleView.h"
#import "WorkerModel.h"
#import "CompanyResumeManagementCell.h"
//#import "JobDetailViewController.h"
#import "WorkerResumeDetailViewController.h"

@interface CompanyResumeManagementViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *allDataArray;
    NSMutableArray *untreatedDataArray;
    NSMutableArray *toCommunicateDataArray;
    NSMutableArray *inappropriateDataArray;
    NSMutableArray *doneDataArray;
    
    NSInteger currentPageAll;
    NSInteger currentPageUntreated;
    NSInteger currentPageToCommunicate;
    NSInteger currentPageInappropriate;
    NSInteger currentPageDone;
    
    LYTitleView *titleView;
}
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) UITableView *jobTableView;;
@end

@implementation CompanyResumeManagementViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我收到的简历";
    
    allDataArray = [NSMutableArray arrayWithCapacity:0];
    untreatedDataArray = [NSMutableArray arrayWithCapacity:0];
    toCommunicateDataArray = [NSMutableArray arrayWithCapacity:0];
    inappropriateDataArray = [NSMutableArray arrayWithCapacity:0];
    doneDataArray = [NSMutableArray arrayWithCapacity:0];
    currentPageAll = 1;
    currentPageUntreated = 1;
    currentPageToCommunicate = 1;
    currentPageInappropriate = 1;
    currentPageDone = 1;
    
    [self initUI];
    [self loadData];
}
- (void)loadData {
    self.noDataView.hidden = YES;
    SVPHUDShow;
    NSString *page = @"1";
    NSString *status = @"0";
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    switch (self.selectIndex) {
        case 0: { // 全部
            status = @"0";
            page = [NSString stringWithFormat:@"%ld", (long)currentPageAll];
        } break;
        case 1: { // 未处理
            status = @"1";
            page = [NSString stringWithFormat:@"%ld", (long)currentPageUntreated];
        } break;
        case 2: { // 待沟通
            status = @"2";
            page = [NSString stringWithFormat:@"%ld", (long)currentPageToCommunicate];
        } break;
        case 3: { // 不合适
            status = @"3";
            page = [NSString stringWithFormat:@"%ld", (long)currentPageInappropriate];
        } break;
        case 4: { // 已邀约复试
            status = @"4";
            page = [NSString stringWithFormat:@"%ld", (long)currentPageDone];
        } break;
        default:
            break;
    }
    [dic setObject:status forKey:@"status"];
    [dic setObject:page forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:Company_GetJobApply_Lists_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dic in data) {
                    WorkerModel *model = [[WorkerModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    model.type = WorkerCellTypeOfOnlyTime;
                    switch (self.selectIndex) {
                        case 0: {
                            [self->allDataArray addObject:model];
                        } break;
                        case 1: {
                            [self->untreatedDataArray addObject:model];
                        } break;
                        case 2: {
                            [self->toCommunicateDataArray addObject:model];
                        } break;
                        case 3: {
                            [self->inappropriateDataArray addObject:model];
                        } break;
                        case 4: {
                            [self->doneDataArray addObject:model];
                        } break;
                        default:
                            break;
                    }
                }
                self.noDataView.hidden = YES;
                [self.jobTableView.mj_footer endRefreshing];
                [self.jobTableView reloadData];
            }
            switch (self.selectIndex) {
                case 0: {
                    if (self->allDataArray.count) {
                    } else {
                        self.noDataView.hidden = NO;
                        [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
                    }
                } break;
                case 1: {
                    if (self->untreatedDataArray.count) {
                    } else {
                        self.noDataView.hidden = NO;
                        [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
                    }
                } break;
                case 2: {
                    if (self->toCommunicateDataArray.count) {
                    } else {
                        self.noDataView.hidden = NO;
                        [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
                    }
                } break;
                case 3: {
                    if (self->inappropriateDataArray.count) {
                    } else {
                        self.noDataView.hidden = NO;
                        [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
                    }
                } break;
                case 4: {
                    if (self->doneDataArray.count) {
                    } else {
                        self.noDataView.hidden = NO;
                        [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
                    }
                } break;
                default:
                    break;
            }
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initUI {
    NSArray *titleArray = @[@"全部", @"未处理", @"待沟通", @"不合适", @"已邀约复试"];
    self.selectIndex = 0;
    titleView = [[LYTitleView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40*Scale) titlesArray:titleArray norCorlor:THEMECOLOR_OF_TEXT_666666 selectColor:THEMECOLOR_OF_ORANGE];
    __weak typeof(self)weakSelf = self;
    titleView.block = ^(NSInteger index){
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSLog(@"%li", (long)index);
        strongSelf.selectIndex = index;
        switch (index) {
            case 0: {
                if (self->allDataArray && self->allDataArray.count) {
                    strongSelf.noDataView.hidden = YES;
                    [strongSelf.jobTableView reloadData];
                } else {
                    [strongSelf loadData];
                }
            } break;
            case 1: {
                if (self->untreatedDataArray && self->untreatedDataArray.count) {
                    strongSelf.noDataView.hidden = YES;
                    [strongSelf.jobTableView reloadData];
                } else {
                    [strongSelf loadData];
                }
            } break;
            case 2: {
                if (self->toCommunicateDataArray && self->toCommunicateDataArray.count) {
                    strongSelf.noDataView.hidden = YES;
                    [strongSelf.jobTableView reloadData];
                } else {
                    [strongSelf loadData];
                }
            } break;
            case 3: {
                if (self->inappropriateDataArray && self->inappropriateDataArray.count) {
                    strongSelf.noDataView.hidden = YES;
                    [strongSelf.jobTableView reloadData];
                } else {
                    [strongSelf loadData];
                }
            } break;
            case 4: {
                if (self->doneDataArray && self->doneDataArray.count) {
                    strongSelf.noDataView.hidden = YES;
                    [strongSelf.jobTableView reloadData];
                } else {
                    [strongSelf loadData];
                }
            } break;
            default:
                break;
        }
    };
    [self.view addSubview:titleView];
    
    self.jobTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, EndY(titleView), WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-ViewHeight(titleView)) target:self style:UITableViewStylePlain];
    self.jobTableView.rowHeight = 138*Scale;
    self.jobTableView.tableFooterView = [[UIView alloc] init];
    self.jobTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.jobTableView];
    
    self.jobTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        switch (self.selectIndex) {
            case 0: {
                self->currentPageAll++;
            } break;
            case 1: {
                self->currentPageUntreated++;
            } break;
            case 2: {
                self->currentPageToCommunicate++;
            } break;
            case 3: {
                self->currentPageInappropriate++;
            } break;
            case 4: {
                self->currentPageDone++;
            } break;
            default:
                break;
        }
        [weakSelf.jobTableView.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (self.selectIndex) {
        case 0: { // 全部
            if (allDataArray.count) {
                return allDataArray.count;
            }
        } break;
        case 1: { // 未处理
            if (untreatedDataArray.count) {
                return untreatedDataArray.count;
            }
        } break;
        case 2: { // 待沟通
            if (toCommunicateDataArray.count) {
                return toCommunicateDataArray.count;
            }
        } break;
        case 3: { // 不合适
            if (inappropriateDataArray.count) {
                return inappropriateDataArray.count;
            }
        } break;
        case 4: { // 已邀约复试
            if (doneDataArray.count) {
                return doneDataArray.count;
            }
        } break;
        default:
            break;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CompanyResumeManagementCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[CompanyResumeManagementCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    switch (self.selectIndex) {
        case 0: { // 全部
            if (allDataArray.count) {
                WorkerModel *model = allDataArray[indexPath.row];
                [cell reloadDataModel:model];
            }
        } break;
        case 1: { // 未处理
            if (untreatedDataArray.count) {
                WorkerModel *model = untreatedDataArray[indexPath.row];
                [cell reloadDataModel:model];
            }
        } break;
        case 2: { // 待沟通
            if (toCommunicateDataArray.count) {
                WorkerModel *model = toCommunicateDataArray[indexPath.row];
                [cell reloadDataModel:model];
            }
        } break;
        case 3: { // 不合适
            if (inappropriateDataArray.count) {
                WorkerModel *model = inappropriateDataArray[indexPath.row];
                [cell reloadDataModel:model];
            }
        } break;
        case 4: { // 已邀约复试
            if (doneDataArray.count) {
                WorkerModel *model = doneDataArray[indexPath.row];
                [cell reloadDataModel:model];
            }
        } break;
        default:
            break;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WorkerResumeDetailViewController *WRDVC = [[WorkerResumeDetailViewController alloc] init];
    switch (self.selectIndex) {
        case 0: {
            WorkerModel *model = allDataArray[indexPath.row];
            WRDVC.id = model.resume_id;
            WRDVC.toudi_id = model.id;
            WRDVC.user_id = model.userid;
            WRDVC.user_name = model.name;
            WRDVC.job_id = model.job_id;
            WRDVC.job_title = model.job_title;
            WRDVC.job_cn = model.intention_jobs;
            WRDVC.wage_cn = model.wage_cn;
        } break;
        case 1: {
            WorkerModel *model = untreatedDataArray[indexPath.row];
            WRDVC.id = model.resume_id;
            WRDVC.toudi_id = model.id;
            WRDVC.user_id = model.userid;
            WRDVC.user_name = model.name;
            WRDVC.job_id = model.job_id;
            WRDVC.job_title = model.job_title;
            WRDVC.job_cn = model.intention_jobs;
            WRDVC.wage_cn = model.wage_cn;
        } break;
        case 2: {
            WorkerModel *model = toCommunicateDataArray[indexPath.row];
            WRDVC.id = model.resume_id;
            WRDVC.toudi_id = model.id;
            WRDVC.user_id = model.userid;
            WRDVC.user_name = model.name;
            WRDVC.job_id = model.job_id;
            WRDVC.job_title = model.job_title;
            WRDVC.job_cn = model.intention_jobs;
            WRDVC.wage_cn = model.wage_cn;
        } break;
        case 3: {
            WorkerModel *model = inappropriateDataArray[indexPath.row];
            WRDVC.id = model.resume_id;
            WRDVC.toudi_id = model.id;
            WRDVC.user_id = model.userid;
            WRDVC.user_name = model.name;
            WRDVC.job_id = model.job_id;
            WRDVC.job_title = model.job_title;
            WRDVC.job_cn = model.intention_jobs;
            WRDVC.wage_cn = model.wage_cn;
        } break;
        case 4: {
            WorkerModel *model = doneDataArray[indexPath.row];
            WRDVC.id = model.resume_id;
            WRDVC.toudi_id = model.id;
            WRDVC.user_id = model.userid;
            WRDVC.user_name = model.name;
            WRDVC.job_id = model.job_id;
            WRDVC.job_title = model.job_title;
            WRDVC.job_cn = model.intention_jobs;
            WRDVC.wage_cn = model.wage_cn;
        } break;
        default:
            break;
    }
    WRDVC.canShowImg = YES;
    WRDVC.showChatSettingFlag = YES;
    WRDVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:WRDVC animated:YES];
}

@end
